package org.wy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.mapstruct.ap.shaded.freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.wy.common.BaseController;
import org.wy.feign.client.CommonFeign;
import org.wy.moudle.CityCodeConstant;
import org.wy.moudle.StateConstant;
import org.wy.service.PwdpResultService;
import org.wy.vo.SstdjcVo;
import org.wy.vo.TdcsVo;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/dw-service-xxl-screen/DldpResultController")
@RequiredArgsConstructor
@Component
@CrossOrigin(origins = "*")
public class PwdpResultController extends BaseController {

    private final PwdpResultService pwdpResultService;

    @Autowired
    private CommonFeign commonFeign;

    private static final String URL = "localhost:9055/dw-service-xxl-screen/ppt/";

    @RequestMapping("jump2screen")
//    @ResponseBody
    public void jump2screen(HttpServletRequest request, HttpServletResponse response, RedirectAttributes attributes) {
        String dwbm = request.getParameter("dwbm");
        System.err.println(dwbm);
        Cookie cookie = new Cookie("dwbm", dwbm);
        cookie.setMaxAge(60000);
//        cookie.setPath("");
        response.addCookie(cookie);
        try {
            response.sendRedirect("../index.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /*
        首页面获取数据Controller
     */
    @RequestMapping("getSecondJson")
    @ResponseBody
    public Map<String, Object> getTableData(HttpServletRequest request, HttpServletResponse response) {
        //获取登录人员单位编码
        String dwbm = "";
        System.out.println("-----------------------------------");
        Cookie[] cookies = request.getCookies();
        System.out.println(cookies.length);
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("Name=" + name + ",Value=" + value);
            if ("dwbm".equals(name)) {
                dwbm = value;
            }
        }
        //Todo 目前默认如果cookie中值为空，只显示辽宁省数据
        if (StringUtils.isBlank(dwbm)){
            dwbm = "012021";
        }
        //封装数据
        //结果map
        Map<String, Object> resultmap = new LinkedHashMap<>();
        //数据封装map
        Map<String, Object> data = new LinkedHashMap<>();
        //===============================================================left
        //1.1 左侧List
        Map<String, Object> left = new LinkedHashMap<>();
        List<SstdjcVo> leftList = getLeftList(left);
        //getLeftChart(left, leftList);//20240130 改为前端直接处理
        data.put("left", left);
        //===============================================================right
        Map<String, Object> right = new LinkedHashMap<>();
        //主动抢修管控
        getRightZdqxgk(right,dwbm);
        //故障报修
        getRightGzbx(right,dwbm);
        data.put("right", right);
        //================================================================filter
        Map<String, Object> filter = new LinkedHashMap<>();
        Map<String, Object> a1 = getFilterMap(dwbm);
        filter.put("a1", a1);
        data.put("filterList", filter);

        //=================================================================map
        Map<String, Object> map = new LinkedHashMap<>();
        //热力图
        getHeatMap(map);
        //实时停电地图
        getTdsjMap(map);
        data.put("map", map);

        resultmap.put("data", data);
        resultmap.put("code", 200);
        return resultmap;
    }

    private Map<String, Object> getFilterMap(String dwbm) {
        Map<String, Object> a1 = new LinkedHashMap<>();
        List<Map<String, Object>> selectOptionsa1 = new ArrayList<>();
        switch (dwbm) {
            case "012021":
                Map<String, Object> selectOptionsaMap1 = new LinkedHashMap<>();
                selectOptionsaMap1.put("label", "辽宁");
                selectOptionsaMap1.put("value", "210000");
                selectOptionsa1.add(selectOptionsaMap1);
                Map<String, Object> selectOptionsaMap2 = new LinkedHashMap<>();
                selectOptionsaMap2.put("label", "沈阳");
                selectOptionsaMap2.put("value", "210100");
                selectOptionsa1.add(selectOptionsaMap2);
                Map<String, Object> selectOptionsaMap3 = new LinkedHashMap<>();
                selectOptionsaMap3.put("label", "大连");
                selectOptionsaMap3.put("value", "210200");
                selectOptionsa1.add(selectOptionsaMap3);
                Map<String, Object> selectOptionsaMap4 = new LinkedHashMap<>();
                selectOptionsaMap4.put("label", "鞍山");
                selectOptionsaMap4.put("value", "210300");
                selectOptionsa1.add(selectOptionsaMap4);
                Map<String, Object> selectOptionsaMap5 = new LinkedHashMap<>();
                selectOptionsaMap5.put("label", "抚顺");
                selectOptionsaMap5.put("value", "210400");
                selectOptionsa1.add(selectOptionsaMap5);
                Map<String, Object> selectOptionsaMap6 = new LinkedHashMap<>();
                selectOptionsaMap6.put("label", "本溪");
                selectOptionsaMap6.put("value", "210500");
                selectOptionsa1.add(selectOptionsaMap6);
                Map<String, Object> selectOptionsaMap7 = new LinkedHashMap<>();
                selectOptionsaMap7.put("label", "丹东");
                selectOptionsaMap7.put("value", "210600");
                selectOptionsa1.add(selectOptionsaMap7);
                Map<String, Object> selectOptionsaMap8 = new LinkedHashMap<>();
                selectOptionsaMap8.put("label", "锦州");
                selectOptionsaMap8.put("value", "210700");
                selectOptionsa1.add(selectOptionsaMap8);
                Map<String, Object> selectOptionsaMap9 = new LinkedHashMap<>();
                selectOptionsaMap9.put("label", "营口");
                selectOptionsaMap9.put("value", "210800");
                selectOptionsa1.add(selectOptionsaMap9);
                Map<String, Object> selectOptionsaMap10 = new LinkedHashMap<>();
                selectOptionsaMap10.put("label", "阜新");
                selectOptionsaMap10.put("value", "210900");
                selectOptionsa1.add(selectOptionsaMap10);
                Map<String, Object> selectOptionsaMap11 = new LinkedHashMap<>();
                selectOptionsaMap11.put("label", "辽阳");
                selectOptionsaMap11.put("value", "211000");
                selectOptionsa1.add(selectOptionsaMap11);
                Map<String, Object> selectOptionsaMap12 = new LinkedHashMap<>();
                selectOptionsaMap12.put("label", "盘锦");
                selectOptionsaMap12.put("value", "211100");
                selectOptionsa1.add(selectOptionsaMap12);
                Map<String, Object> selectOptionsaMap13 = new LinkedHashMap<>();
                selectOptionsaMap13.put("label", "铁岭");
                selectOptionsaMap13.put("value", "211200");
                selectOptionsa1.add(selectOptionsaMap13);
                Map<String, Object> selectOptionsaMap14 = new LinkedHashMap<>();
                selectOptionsaMap14.put("label", "朝阳");
                selectOptionsaMap14.put("value", "211300");
                selectOptionsa1.add(selectOptionsaMap14);
                Map<String, Object> selectOptionsaMap15 = new LinkedHashMap<>();
                selectOptionsaMap15.put("label", "葫芦岛");
                selectOptionsaMap15.put("value", "211400");
                selectOptionsa1.add(selectOptionsaMap15);
                break;
            case "01202121501":
                Map<String, Object> selectOptionsaMapCity1 = new LinkedHashMap<>();
                selectOptionsaMapCity1.put("label", "沈阳");
                selectOptionsaMapCity1.put("value", "210100");
                selectOptionsa1.add(selectOptionsaMapCity1);
                break;
            case "01202121508":
                Map<String, Object> selectOptionsaMapCity2 = new LinkedHashMap<>();
                selectOptionsaMapCity2.put("label", "大连");
                selectOptionsaMapCity2.put("value", "210200");
                selectOptionsa1.add(selectOptionsaMapCity2);
                break;
            default:
                Map<String, Object> selectOptionsaMapCity00 = new LinkedHashMap<>();
                selectOptionsaMapCity00.put("label", "辽宁");
                selectOptionsaMapCity00.put("value", "210000");
                selectOptionsa1.add(selectOptionsaMapCity00);
                break;
        }
        a1.put("selectOptions", selectOptionsa1);
        return a1;
    }

    /*
    首页面获取数据Controller
 */
    @GetMapping("getTdxxxq/{objId}")
    @ResponseBody
    public Map<String, Object> getTdxxxq(@PathVariable("objId") String objId) {

        System.out.println(objId);

        //结果map
        Map<String, Object> listmapdata = new LinkedHashMap<>();
        Map<String, Object> resultdata = new LinkedHashMap<>();

        JSONObject tdsjxx = commonFeign.getTdsjxx(objId);
        JSONObject data = tdsjxx.getJSONObject("data");
        //base 基本信息
        listmapdata = new LinkedHashMap<>();
        listmapdata.put("psrId", data.getString("dkxid"));
        List<List<Object>> listmapdataList = new ArrayList<>();
        List<Object> listmapdataListbaseList = new ArrayList<>();
        listmapdataListbaseList.add("线路名称");
        listmapdataListbaseList.add(data.getString("xlmc"));
        listmapdataList.add(listmapdataListbaseList);
        List<Object> listmapdataListbaseList1 = new ArrayList<>();
        listmapdataListbaseList1.add("所属地市");
        listmapdataListbaseList1.add(data.getString("ds"));
        listmapdataList.add(listmapdataListbaseList1);
        List<Object> listmapdataListbaseList2 = new ArrayList<>();
        listmapdataListbaseList2.add("所属区县");
        listmapdataListbaseList2.add(data.getString("qx"));
        listmapdataList.add(listmapdataListbaseList2);
        List<Object> listmapdataListbaseList3 = new ArrayList<>();
        listmapdataListbaseList3.add("所属班所");
        listmapdataListbaseList3.add(data.getString("dwmc"));
        listmapdataList.add(listmapdataListbaseList3);
        List<Object> listmapdataListbaseList4 = new ArrayList<>();
        listmapdataListbaseList4.add("台区名称");
        listmapdataListbaseList4.add(data.getString("yhmc"));
        listmapdataList.add(listmapdataListbaseList4);
        List<Object> listmapdataListbaseList5 = new ArrayList<>();
        listmapdataListbaseList5.add("停电时间");
        listmapdataListbaseList5.add(data.getString("kssj"));
        listmapdataList.add(listmapdataListbaseList5);
        List<Object> listmapdataListbaseList6 = new ArrayList<>();
        listmapdataListbaseList6.add("复电时间");
        listmapdataListbaseList6.add(data.getString("fdsj"));
        listmapdataList.add(listmapdataListbaseList6);
        List<Object> listmapdataListbaseList7 = new ArrayList<>();
        listmapdataListbaseList7.add("停电时长");
        listmapdataListbaseList7.add(new BigDecimal(data.getString("cxsj")).setScale(3, RoundingMode.HALF_UP) + " h");
        listmapdataList.add(listmapdataListbaseList7);
        List<Object> listmapdataListbaseList8 = new ArrayList<>();
        listmapdataListbaseList8.add("总台区数");
        listmapdataListbaseList8.add(data.getFloat("zyhs"));
        listmapdataList.add(listmapdataListbaseList8);
        List<Object> listmapdataListbaseList9 = new ArrayList<>();
        listmapdataListbaseList9.add("停电台区数");
        listmapdataListbaseList9.add(Float.valueOf(data.getJSONArray("tdtq").size()));
        listmapdataList.add(listmapdataListbaseList9);
        List<Object> listmapdataListbaseList10 = new ArrayList<>();
        listmapdataListbaseList10.add("复电台区数");
        listmapdataListbaseList10.add(data.getFloat("fdhs"));
        listmapdataList.add(listmapdataListbaseList10);
        List<Object> listmapdataListbaseList11 = new ArrayList<>();
        listmapdataListbaseList11.add("未复电台区数");
        listmapdataListbaseList11.add(Float.valueOf(data.getJSONArray("tdtq").size() - data.getInteger("fdhs")));
        listmapdataList.add(listmapdataListbaseList11);
        List<Object> listmapdataListbaseList12 = new ArrayList<>();
        listmapdataListbaseList12.add("影响低压用户数");
        listmapdataListbaseList12.add(data.getFloat("yxdys"));
        listmapdataList.add(listmapdataListbaseList12);
        List<Object> listmapdataListbaseList13 = new ArrayList<>();
        listmapdataListbaseList13.add("停电类型");
        listmapdataListbaseList13.add(data.getString("tdlb"));
        listmapdataList.add(listmapdataListbaseList13);
        List<Object> listmapdataListbaseList14 = new ArrayList<>();
        listmapdataListbaseList14.add("停电性质");
        listmapdataListbaseList14.add(data.getString("tdxz"));
        listmapdataList.add(listmapdataListbaseList14);
        List<Object> listmapdataListbaseList15 = new ArrayList<>();
        listmapdataListbaseList15.add("停复电状态");
        listmapdataListbaseList15.add(data.getString("fdzt"));
        listmapdataList.add(listmapdataListbaseList15);
        List<Object> listmapdataListbaseList16 = new ArrayList<>();
        listmapdataListbaseList16.add("数据状态");
        listmapdataListbaseList16.add(data.getString("sjzt"));
        listmapdataList.add(listmapdataListbaseList16);

        listmapdata.put("base", listmapdataList);

        //judgment 研判信息
        List<Map<String, Object>> judgmentdataList = new ArrayList<>();
        Map<String, Object> judgmentdataMap = new LinkedHashMap<>();
        List<Object> judgmentdataTopList = new ArrayList<>();
        judgmentdataTopList.add("自动采集");
        judgmentdataTopList.add(data.getString("zdcj"));
        judgmentdataTopList.add("户");
        judgmentdataMap.put("top", judgmentdataTopList);
        List<Object> judgmentdataBottomList = new ArrayList<>();
        judgmentdataBottomList.add("采集入库时间");
        judgmentdataBottomList.add(StringUtils.isBlank(data.getString("zdcjsj")) ? "---" : data.getString("zdcjsj"));
        judgmentdataMap.put("bottom", judgmentdataBottomList);
        judgmentdataList.add(judgmentdataMap);
        Map<String, Object> judgmentdataMap1 = new LinkedHashMap<>();
        List<Object> judgmentdataTopList1 = new ArrayList<>();
        judgmentdataTopList1.add("自动补全");
        judgmentdataTopList1.add(data.getString("zdbq"));
        judgmentdataTopList1.add("户");
        judgmentdataMap1.put("top", judgmentdataTopList1);
        List<Object> judgmentdataBottomList1 = new ArrayList<>();
        judgmentdataBottomList1.add("补全入库时间");
        judgmentdataBottomList1.add(StringUtils.isBlank(data.getString("zdbqsj")) ? "---" : data.getString("zdbqsj"));
        judgmentdataMap1.put("bottom", judgmentdataBottomList1);
        judgmentdataList.add(judgmentdataMap1);
        Map<String, Object> judgmentdataMap2 = new LinkedHashMap<>();
        List<Object> judgmentdataTopList2 = new ArrayList<>();
        judgmentdataTopList2.add("营销召测");
        judgmentdataTopList2.add(data.getString("yhs_yxzc"));
        judgmentdataTopList2.add("户");
        judgmentdataMap2.put("top", judgmentdataTopList2);
        List<Object> judgmentdataBottomList2 = new ArrayList<>();
        judgmentdataBottomList2.add("召测入库时间");
        judgmentdataBottomList2.add(StringUtils.isBlank(data.getString("yxzc_time")) ? "---" : data.getString("zdbqsj"));
        judgmentdataMap2.put("bottom", judgmentdataBottomList2);
        judgmentdataList.add(judgmentdataMap2);
        Map<String, Object> judgmentdataMap3 = new LinkedHashMap<>();
        List<Object> judgmentdataTopList3 = new ArrayList<>();
        judgmentdataTopList3.add("主线开关信号");
        judgmentdataTopList3.add("0");
        judgmentdataTopList3.add("户");
        judgmentdataMap3.put("top", judgmentdataTopList3);
        List<Object> judgmentdataBottomList3 = new ArrayList<>();
        judgmentdataBottomList3.add("开关动作时间");
        judgmentdataBottomList3.add("---");
        judgmentdataMap3.put("bottom", judgmentdataBottomList3);
        judgmentdataList.add(judgmentdataMap3);
        Map<String, Object> judgmentdataMap4 = new LinkedHashMap<>();
        List<Object> judgmentdataTopList4 = new ArrayList<>();
        judgmentdataTopList4.add("自动化开关信号");
        judgmentdataTopList4.add("0");
        judgmentdataTopList4.add("户");
        judgmentdataMap4.put("top", judgmentdataTopList4);
        List<Object> judgmentdataBottomList4 = new ArrayList<>();
        judgmentdataBottomList4.add("开关动作时间");
        judgmentdataBottomList4.add("---");
        judgmentdataMap4.put("bottom", judgmentdataBottomList4);
        judgmentdataList.add(judgmentdataMap4);
//        Map<String, Object> judgmentdataMap5 = new LinkedHashMap<>();
//        List<Object> judgmentdataTopList5 = new ArrayList<>();
//        judgmentdataTopList5.add("自动化开关信号");
//        judgmentdataTopList5.add("0");
//        judgmentdataTopList5.add("户");
//        judgmentdataMap5.put("top", judgmentdataTopList5);
//        List<Object> judgmentdataBottomList5 = new ArrayList<>();
//        judgmentdataBottomList5.add("开关动作时间");
//        judgmentdataBottomList5.add("---");
//        judgmentdataMap5.put("bottom", judgmentdataBottomList5);
//        judgmentdataList.add(judgmentdataMap5);
        Map<String, Object> judgmentdataMap6 = new LinkedHashMap<>();
        List<Object> judgmentdataTopList6 = new ArrayList<>();
        judgmentdataTopList6.add("电压校正");
        judgmentdataTopList6.add("0");
        judgmentdataTopList6.add("户");
        judgmentdataMap6.put("top", judgmentdataTopList6);
        List<Object> judgmentdataBottomList6 = new ArrayList<>();
        judgmentdataBottomList6.add("电压校正时间");
        judgmentdataBottomList6.add("---");
        judgmentdataMap6.put("bottom", judgmentdataBottomList6);
        judgmentdataList.add(judgmentdataMap6);
        Map<String, Object> judgmentdataMap7 = new LinkedHashMap<>();
        List<Object> judgmentdataTopList7 = new ArrayList<>();
        judgmentdataTopList7.add("电流校正");
        judgmentdataTopList7.add("0");
        judgmentdataTopList7.add("户");
        judgmentdataMap7.put("top", judgmentdataTopList7);
        List<Object> judgmentdataBottomList7 = new ArrayList<>();
        judgmentdataBottomList7.add("电流校正时间");
        judgmentdataBottomList7.add("---");
        judgmentdataMap7.put("bottom", judgmentdataBottomList7);
        judgmentdataList.add(judgmentdataMap7);
        Map<String, Object> judgmentdataMap8 = new LinkedHashMap<>();
        List<Object> judgmentdataTopList8 = new ArrayList<>();
        judgmentdataTopList8.add("停电台区");
        judgmentdataTopList8.add("0");
        judgmentdataTopList8.add("户");
        judgmentdataMap8.put("top", judgmentdataTopList8);
        List<Object> judgmentdataBottomList8 = new ArrayList<>();
        judgmentdataBottomList8.add("");
        judgmentdataBottomList8.add("");
        judgmentdataMap8.put("bottom", judgmentdataBottomList8);
        judgmentdataList.add(judgmentdataMap8);
        listmapdata.put("judgment", judgmentdataList);

        //Todo repair 抢修进度 需求待确定，目前不调用接口，返回固定格式
        List<Map<String, Object>> repairdataList = new ArrayList<>();
        Map<String, Object> repairdataMap = new LinkedHashMap<>();
        List<Object> repairdataTopList = new ArrayList<>();
        repairdataTopList.add("工单派发");
        repairdataTopList.add("");
        repairdataTopList.add("");
        repairdataMap.put("top", repairdataTopList);
        List<Object> repairdataBottomList = new ArrayList<>();
        repairdataBottomList.add("");
        repairdataBottomList.add("-");
        repairdataMap.put("bottom", repairdataBottomList);
        repairdataList.add(repairdataMap);
        Map<String, Object> repairdataMap1 = new LinkedHashMap<>();
        List<Object> repairdataTopList1 = new ArrayList<>();
        repairdataTopList1.add("到达现场");
        repairdataTopList1.add("");
        repairdataTopList1.add("");
        repairdataMap1.put("top", repairdataTopList1);
        List<Object> repairdataBottomList1 = new ArrayList<>();
        repairdataBottomList1.add("");
        repairdataBottomList1.add("-");
        repairdataMap1.put("bottom", repairdataBottomList1);
        repairdataList.add(repairdataMap1);
        Map<String, Object> repairdataMap2 = new LinkedHashMap<>();
        List<Object> repairdataTopList2 = new ArrayList<>();
        repairdataTopList2.add("发现故障点");
        repairdataTopList2.add("");
        repairdataTopList2.add("");
        repairdataMap2.put("top", repairdataTopList2);
        List<Object> repairdataBottomList2 = new ArrayList<>();
        repairdataBottomList2.add("");
        repairdataBottomList2.add("-");
        repairdataMap2.put("bottom", repairdataBottomList2);
        repairdataList.add(repairdataMap2);
        Map<String, Object> repairdataMap3 = new LinkedHashMap<>();
        List<Object> repairdataTopList3 = new ArrayList<>();
        repairdataTopList3.add("隔离故障点");
        repairdataTopList3.add("");
        repairdataTopList3.add("");
        repairdataMap3.put("top", repairdataTopList3);
        List<Object> repairdataBottomList3 = new ArrayList<>();
        repairdataBottomList3.add("");
        repairdataBottomList3.add("-");
        repairdataMap3.put("bottom", repairdataBottomList3);
        repairdataList.add(repairdataMap3);
        Map<String, Object> repairdataMap4 = new LinkedHashMap<>();
        List<Object> repairdataTopList4 = new ArrayList<>();
        repairdataTopList4.add("修复完成");
        repairdataTopList4.add("");
        repairdataTopList4.add("");
        repairdataMap4.put("top", repairdataTopList4);
        List<Object> repairdataBottomList4 = new ArrayList<>();
        repairdataBottomList4.add("");
        repairdataBottomList4.add("-");
        repairdataMap4.put("bottom", repairdataBottomList4);
        repairdataList.add(repairdataMap4);
        Map<String, Object> repairdataMap5 = new LinkedHashMap<>();
        List<Object> repairdataTopList5 = new ArrayList<>();
        repairdataTopList5.add("现场送电");
        repairdataTopList5.add("");
        repairdataTopList5.add("");
        repairdataMap5.put("top", repairdataTopList5);
        List<Object> repairdataBottomList5 = new ArrayList<>();
        repairdataBottomList5.add("");
        repairdataBottomList5.add("-");
        repairdataMap5.put("bottom", repairdataBottomList5);
        repairdataList.add(repairdataMap5);
        listmapdata.put("repair", repairdataList);

        resultdata.put("data", listmapdata);
        resultdata.put("code", 200);

        return listmapdata;
    }

    private void getTdsjMap(Map<String, Object> map) {
        String date = getcurrentdate();
        //按照目前方案，如省公司账户，需要调用15次接口，先按照此方案测试效率
        List<Map<String, Object>> blackoutdata = new ArrayList<>();
        //1、全省数据
        JSONObject tdcsAll = commonFeign.getTdcs("012021", "0", date, "");
        JSONObject data = tdcsAll.getJSONObject("data");
        JSONArray pjtdsj = data.getJSONArray("pjtdsj");
        List<TdcsVo> csList = pjtdsj.toJavaList(TdcsVo.class);
        //计算今日停电事件总数
        int sum = csList.stream().mapToInt(TdcsVo::getTdsj).sum();
        for (TdcsVo tdcsVo : csList) {
            Map<String, Object> blackoutdataMap = new LinkedHashMap<>();
            String adcode = null;
            switch (tdcsVo.getDwjc()) {
                case "沈阳":
                    adcode = "210100";
                    break;
                case "大连":
                    adcode = "210200";
                    break;
                case "鞍山":
                    adcode = "210300";
                    break;
                case "抚顺":
                    adcode = "210400";
                    break;
                case "本溪":
                    adcode = "210500";
                    break;
                case "丹东":
                    adcode = "210600";
                    break;
                case "锦州":
                    adcode = "210700";
                    break;
                case "营口":
                    adcode = "210800";
                    break;
                case "阜新":
                    adcode = "210900";
                    break;
                case "辽阳":
                    adcode = "211000";
                    break;
                case "盘锦":
                    adcode = "211100";
                    break;
                case "铁岭":
                    adcode = "211200";
                    break;
                case "朝阳":
                    adcode = "211300";
                    break;
                case "葫芦岛":
                    adcode = "211400";
                    break;
            }
            ;

            blackoutdataMap.put("adcode", adcode);
            blackoutdataMap.put("value", tdcsVo.getTdsj());
            blackoutdataMap.put("value2", tdcsVo.getFdsj());
            blackoutdataMap.put("color", getcolor(tdcsVo.getTdsj()));
            blackoutdata.add(blackoutdataMap);
        }

        Map<String, Object> blackout = new LinkedHashMap<>();
        Map<String, Object> blackoutvalue = new LinkedHashMap<>();
        List<Map<String, Object>> blackoutdataList = new ArrayList<>();

        //设置阈值颜色提醒
        List<Map<String, Object>> blackoutlimit = new ArrayList<>();
        Map<String, Object> blackoutlimitMap = new LinkedHashMap<>();
        blackoutlimitMap.put("rang", "0-10");
        blackoutlimitMap.put("color", "#40fd0c");
        blackoutlimit.add(blackoutlimitMap);
        Map<String, Object> blackoutlimitMap1 = new LinkedHashMap<>();
        blackoutlimitMap1.put("rang", "10-20");
        blackoutlimitMap1.put("color", "#fdb90c");
        blackoutlimit.add(blackoutlimitMap1);
        Map<String, Object> blackoutlimitMap2 = new LinkedHashMap<>();
        blackoutlimitMap2.put("rang", "20以上");
        blackoutlimitMap2.put("color", "#ff0000");
        blackoutlimit.add(blackoutlimitMap2);
        blackout.put("limit", blackoutlimit);
        //2、各省市数据
        //2。1 获取沈阳数据 13
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.SHENYANG));
        //2。2 获取大连数据 10+1
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.DALIAN));
        //2。3 鞍山 6+1
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.ANSHAN));
        //2。4 抚顺 7
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.FUSHUN));
        //2。5 本溪 6
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.BENXI));
        //2。6 丹东 6
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.DANDONG));
        //2。7 锦州 6+1
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.JINZHOU));
        //2。8 营口 6
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.YINGKOU));
        //2。9 阜新 6+1
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.FUXIN));
        //2。10 辽阳 6+1
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.LIAOYANG));
        //2。11 盘锦 4
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.PANJIN));
        //2。12 铁岭 7
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.TIELING));
        //2。13 朝阳 7
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.CHAOYANG));
        //2。14 葫芦岛 5+1
        blackoutdataList.add(getCitytdsj(date, blackoutdata, CityCodeConstant.HULUDAO));
        //对于前端JSON有地区码但接口内无返回对应地区的，默认为0处理
        String[] nomatchqx = {"210203", "210311", "210781", "210903", "211003", "211402"};
        for (String adcode : nomatchqx) {
            Map blackoutdatacityMap = new LinkedHashMap();
            blackoutdatacityMap.put("adcode", adcode);
            blackoutdatacityMap.put("value", 0);
            blackoutdatacityMap.put("value2", 0);
            blackoutdatacityMap.put("color", getcolor(0));
            blackoutdata.add(blackoutdatacityMap);
        }

        Map<String, Object> lndata = new LinkedHashMap();
        lndata.put("adcode","210000");
        lndata.put("value",sum);
        blackoutdataList.add(lndata);

        blackoutvalue.put("label", "今日停电事件");
        //Todo 结构改变 一次性传值
        blackoutvalue.put("data", blackoutdataList);
        blackoutvalue.put("unit", "件");
        blackout.put("value", blackoutvalue);


        blackout.put("data", blackoutdata);
        map.put("blackout", blackout);
    }

    private Map getCitytdsj(String date, List<Map<String, Object>> blackoutdata, String citycode) {

        Map tdsjDataMap = new LinkedHashMap();

        JSONObject tdcscity = commonFeign.getTdcs(citycode, "0", date, "");
        JSONObject citydata = tdcscity.getJSONObject("data");
        JSONArray citypjtdsj = citydata.getJSONArray("pjtdsj");
        List<TdcsVo> csCityList = citypjtdsj.toJavaList(TdcsVo.class);
        for (TdcsVo tdcscityVo : csCityList) {
            Map<String, Object> blackoutdatacityMap = new LinkedHashMap<>();
            String qxadcode = null;
            if (CityCodeConstant.SHENYANG.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "新民市":
                        qxadcode = "210181";
                        break;
                    case "辽中区":
                        qxadcode = "210115";
                        break;
                    case "于洪区":
                        qxadcode = "210114";
                        break;
                    case "苏家屯区":
                        qxadcode = "210111";
                        break;
                    case "沈北新区":
                        qxadcode = "210113";
                        break;
                    case "法库县":
                        qxadcode = "210124";
                        break;
                    case "康平县":
                        qxadcode = "210123";
                        break;
                    case "沈河区":
                        qxadcode = "210103";
                        break;
                    case "浑南区":
                        qxadcode = "210112";
                        break;
                    case "皇姑区":
                        qxadcode = "210105";
                        break;
                    case "铁西区":
                        qxadcode = "210106";
                        break;
                    case "大东区":
                        qxadcode = "210104";
                        break;
                    case "和平区":
                        qxadcode = "210102";
                        break;
                }
            } else if (CityCodeConstant.DALIAN.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "中山区":
                        qxadcode = "210202";
                        break;
                    case "沙河口":
                        qxadcode = "210204";
                        break;
                    case "甘井子区":
                        qxadcode = "210211";
                        break;
//                        case "高新园区":
//                            qxadcode = "210111";
//                            break;
                    case "金州新区":
                        qxadcode = "210213";
                        break;
                    case "旅顺口区":
                        qxadcode = "210212";
                        break;
                    case "瓦房店市":
                        qxadcode = "210281";
                        break;
                    case "普兰店区":
                        qxadcode = "210214";
                        break;
                    case "庄河市":
                        qxadcode = "210283";
                        break;
//                        case "开发区":
//                            qxadcode = "210105";
//                            break;
//                        case "开发区东部":
//                            qxadcode = "210106";
//                            break;
//                        case "长兴岛区":
//                            qxadcode = "210104";
//                            break;
                    case "长海县":
                        qxadcode = "210224";
                        break;
                }
            } else if (CityCodeConstant.ANSHAN.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "铁东区":
                        qxadcode = "210302";
                        break;
                    case "铁西区":
                        qxadcode = "210303";
                        break;
                    case "立山区":
                        qxadcode = "210304";
                        break;
//                    case "高新区":
//                        qxadcode = "210213";
//                        break;
                    case "海城市":
                        qxadcode = "210381";
                        break;
                    case "岫岩县":
                        qxadcode = "210323";
                        break;
                    case "台安县":
                        qxadcode = "210321";
                        break;
                }
            } else if (CityCodeConstant.FUSHUN.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
//                    case "开发区":
//                        qxadcode = "210302";
//                        break;
                    case "新抚区":
                        qxadcode = "210402";
                        break;
                    case "望花区":
                        qxadcode = "210404";
                        break;
                    case "顺城区":
                        qxadcode = "210411";
                        break;
                    case "东洲区":
                        qxadcode = "210403";
                        break;
                    case "抚顺县":
                        qxadcode = "210421";
                        break;
                    case "新宾县":
                        qxadcode = "210422";
                        break;
                    case "清原县":
                        qxadcode = "210423";
                        break;
                }
            } else if (CityCodeConstant.BENXI.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "明山区":
                        qxadcode = "210504";
                        break;
                    case "本溪县":
                        qxadcode = "210521";
                        break;
                    case "桓仁县":
                        qxadcode = "210522";
                        break;
                    case "溪湖区":
                        qxadcode = "210503";
                        break;
                    case "南芬区":
                        qxadcode = "210505";
                        break;
                    case "平山区":
                        qxadcode = "210502";
                        break;
                }
            } else if (CityCodeConstant.DANDONG.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "宽甸县":
                        qxadcode = "210624";
                        break;
                    case "振兴区":
                        qxadcode = "210603";
                        break;
                    case "元宝区":
                        qxadcode = "210602";
                        break;
                    case "振安区":
                        qxadcode = "210604";
                        break;
                    case "东港市":
                        qxadcode = "210681";
                        break;
                    case "凤城市":
                        qxadcode = "210682";
                        break;
                }
            } else if (CityCodeConstant.JINZHOU.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "北镇市":
                        qxadcode = "210782";
                        break;
                    case "黑山县":
                        qxadcode = "210726";
                        break;
                    case "义县":
                        qxadcode = "210727";
                        break;
                    case "太和区":
                        qxadcode = "210711";
                        break;
                    case "凌河区":
                        qxadcode = "210703";
                        break;
//                    case "开发区":
//                        qxadcode = "210682";
//                        break;
                    case "古塔区":
                        qxadcode = "210702";
                        break;
                }
            } else if (CityCodeConstant.YINGKOU.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "站前区":
                        qxadcode = "210802";
                        break;
                    case "西市区":
                        qxadcode = "210803";
                        break;
//                    case "熊岳区":
//                        qxadcode = "210727";
//                        break;
                    case "鲅鱼圈区":
                        qxadcode = "210804";
                        break;
                    case "大石桥市":
                        qxadcode = "210882";
                        break;
                    case "盖州市":
                        qxadcode = "210881";
                        break;
                    case "老边区":
                        qxadcode = "210811";
                        break;
                }
            } else if (CityCodeConstant.FUXIN.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "海州区":
                        qxadcode = "210902";
                        break;
//                    case "市南区":
//                        qxadcode = "210803";
//                        break;
                    case "太平区":
                        qxadcode = "210904";
                        break;
                    case "阜蒙县":
                        qxadcode = "210921";
                        break;
                    case "彰武县":
                        qxadcode = "210922";
                        break;
                    case "清河门区":
                        qxadcode = "210905";
                        break;
                    case "细河区":
                        qxadcode = "210911";
                        break;
                }
            } else if (CityCodeConstant.LIAOYANG.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "白塔区":
                        qxadcode = "211002";
                        break;
                    case "辽阳县":
                        qxadcode = "211021";
                        break;
                    case "灯塔市":
                        qxadcode = "211081";
                        break;
                    case "太子河区":
                        qxadcode = "211011";
                        break;
                    case "弓长岭区":
                        qxadcode = "211005";
                        break;
                    case "宏伟区":
                        qxadcode = "211004";
                        break;
                }
            } else if (CityCodeConstant.PANJIN.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "大洼区":
                        qxadcode = "211104";
                        break;
                    case "盘山县":
                        qxadcode = "211122";
                        break;
//                    case "辽东湾新区":
//                        qxadcode = "211081";
//                        break;
                    case "双台子区":
                        qxadcode = "211102";
                        break;
                    case "兴隆台区":
                        qxadcode = "211103";
                        break;
                }
            } else if (CityCodeConstant.TIELING.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "银州区":
                        qxadcode = "211202";
                        break;
//                    case "开发区":
//                        qxadcode = "211122";
//                        break;
                    case "开原市":
                        qxadcode = "211282";
                        break;
                    case "清河区":
                        qxadcode = "211204";
                        break;
                    case "西丰县":
                        qxadcode = "211223";
                        break;
                    case "调兵山市":
                        qxadcode = "211281";
                        break;
                    case "铁岭县":
                        qxadcode = "211221";
                        break;
                    case "昌图县":
                        qxadcode = "211224";
                        break;
                }
            } else if (CityCodeConstant.CHAOYANG.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "建平县":
                        qxadcode = "211322";
                        break;
//                    case "开发区":
//                        qxadcode = "211122";
//                        break;
                    case "凌源市":
                        qxadcode = "211382";
                        break;
                    case "北票市":
                        qxadcode = "211381";
                        break;
                    case "双塔区":
                        qxadcode = "211302";
                        break;
                    case "龙城区":
                        qxadcode = "211303";
                        break;
                    case "朝阳县":
                        qxadcode = "211321";
                        break;
                    case "喀左县":
                        qxadcode = "211324";
                        break;
                }
            } else if (CityCodeConstant.HULUDAO.equals(citycode)) {
                switch (tdcscityVo.getDwjc()) {
                    case "绥中县":
                        qxadcode = "211421";
                        break;
                    case "兴城市":
                        qxadcode = "211481";
                        break;
//                    case "郊区":
//                        qxadcode = "211381";
//                        break;
                    case "建昌县":
                        qxadcode = "211422";
                        break;
                    case "龙港区":
                        qxadcode = "211403";
                        break;
                    case "南票区":
                        qxadcode = "211404";
                        break;
                }
            }
            if (qxadcode != null) {
                blackoutdatacityMap.put("adcode", qxadcode);
                blackoutdatacityMap.put("value", tdcscityVo.getTdsj());
                blackoutdatacityMap.put("value2", tdcscityVo.getFdsj());
                blackoutdatacityMap.put("color", getcolor(tdcscityVo.getTdsj()));
                blackoutdata.add(blackoutdatacityMap);
            }
        }
        //Todo 计算地市总数
        BigDecimal sum = blackoutdata.stream().map(x -> new BigDecimal(String.valueOf(x.get("value")))).reduce(BigDecimal.ZERO,BigDecimal::add);
        String adcode = getAdcodeByDwbm(citycode);
        tdsjDataMap.put("adcode",adcode);
        tdsjDataMap.put("value",sum);
        return tdsjDataMap;

    }

    private String getcolor(Integer tdsj) {
        if (tdsj < 10) {
            return "#40fd0c";
        } else if (tdsj >= 10 && tdsj < 20) {
            return "#fdb90c";
        } else {
            return "#ff0000";
        }
    }

    private void getHeatMap(Map<String, Object> map) {

        JSONArray yhxx = null;
        JSONArray jrdxx = null;
        try {
            JSONObject allTdyh = commonFeign.getAllTdyh();
            JSONObject data = allTdyh.getJSONObject("data");
            yhxx = data.getJSONArray("yhxx");
            jrdxx = data.getJSONArray("jrdxx");
        } catch (Exception e) {
            System.out.println("热力图接口调用异常" + e.getCause());
        }

        Map<String, Object> heatmap = new LinkedHashMap<>();
        Map<String, Object> heatmapdata = new LinkedHashMap<>();

        heatmapdata.put("yhxx", yhxx);
        heatmapdata.put("jrdxx", jrdxx);

        heatmap.put("label", "10kV线路频繁停电");
        heatmap.put("data", heatmapdata);
        map.put("heatmap", heatmap);
    }


    /**
     * right--故障报修管控
     *
     * @param right
     * @param dwbm
     */
    private void getRightGzbx(Map<String, Object> right, String dwbm) {
        if (CityCodeConstant.LIAONING.equals(dwbm)){
            //查询辽宁省数据
            String adcodeByDwbm = getAdcodeByDwbm(dwbm);
            getRightGzbxByDwbm(right,dwbm,adcodeByDwbm);
            //查询地市数据
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.SHENYANG);
            getRightGzbxByDwbm(right,CityCodeConstant.SHENYANG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.DALIAN);
            getRightGzbxByDwbm(right,CityCodeConstant.DALIAN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.ANSHAN);
            getRightGzbxByDwbm(right,CityCodeConstant.ANSHAN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.FUSHUN);
            getRightGzbxByDwbm(right,CityCodeConstant.FUSHUN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.BENXI);
            getRightGzbxByDwbm(right,CityCodeConstant.BENXI,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.DANDONG);
            getRightGzbxByDwbm(right,CityCodeConstant.DANDONG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.JINZHOU);
            getRightGzbxByDwbm(right,CityCodeConstant.JINZHOU,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.YINGKOU);
            getRightGzbxByDwbm(right,CityCodeConstant.YINGKOU,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.FUXIN);
            getRightGzbxByDwbm(right,CityCodeConstant.FUXIN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.LIAOYANG);
            getRightGzbxByDwbm(right,CityCodeConstant.LIAOYANG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.PANJIN);
            getRightGzbxByDwbm(right,CityCodeConstant.PANJIN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.TIELING);
            getRightGzbxByDwbm(right,CityCodeConstant.TIELING,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.CHAOYANG);
            getRightGzbxByDwbm(right,CityCodeConstant.CHAOYANG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.HULUDAO);
            getRightGzbxByDwbm(right,CityCodeConstant.HULUDAO,adcodeByDwbm);
        } else {
            String adcodeByDwbm = getAdcodeByDwbm(dwbm);
            getRightGzbxByDwbm(right,dwbm,adcodeByDwbm);
        }
    }

    private void getRightGzbxByDwbm(Map<String, Object> right,String dwbm,String adcodeByDwbm) {
        Map<String, Object> adcode = (Map<String, Object>) right.get(adcodeByDwbm);

        String date = getcurrentdate();
        Boolean isok = true;
        JSONObject gzqxTjByZtVO = null;
        try {
            System.out.println("==============开始调用:" + "故障报修管控接口" + "=================dwbm:"+ dwbm);
            JSONObject gzqxtj = commonFeign.getGzqxtj(date, dwbm);
            JSONObject data = gzqxtj.getJSONObject("data");
            gzqxTjByZtVO = data.getJSONObject("gzqxTjByZtVO");
            isok = false;
        } catch (Exception e) {
            System.err.println(e.getCause());
            isok = true;
        }

        Map<String, Object> bottom = new LinkedHashMap<>();
        Map<String, Object> data1 = new LinkedHashMap<>();
        data1.put("label", "今日95598故障报修");
        data1.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("num"));
        data1.put("unit", '件');
        bottom.put("data1", data1);

        List<List<Map<String, Object>>> data2 = new ArrayList<>();
        List<Map<String, Object>> data2list = new ArrayList<>();
        Map<String, Object> data2Map1 = new LinkedHashMap<>();
        data2Map1.put("name", "高压故障");
        data2Map1.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("gygznum"));
        data2Map1.put("unit", "件");
        data2Map1.put("total", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("gygznum"));
        data2list.add(data2Map1);
        Map<String, Object> data2Map2 = new LinkedHashMap<>();
        data2Map2.put("name", "低压故障");
        data2Map2.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("dygznum"));
        data2Map2.put("unit", "件");
        data2Map2.put("total", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("dygznum"));
        data2list.add(data2Map2);
        Map<String, Object> data2Map3 = new LinkedHashMap<>();
        data2Map3.put("name", "电能质量");
        data2Map3.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("dnzlnum"));
        data2Map3.put("unit", "件");
        data2Map3.put("total", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("dnzlnum"));
        data2list.add(data2Map3);
        Map<String, Object> data2Map4 = new LinkedHashMap<>();
        data2Map4.put("name", "非电力故障");
        data2Map4.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("fdlgznum"));
        data2Map4.put("unit", "件");
        data2Map4.put("total", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("fdlgznum"));
        data2list.add(data2Map4);
        Map<String, Object> data2Map5 = new LinkedHashMap<>();
        data2Map5.put("name", "计量故障");
        data2Map5.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("jlgznum"));
        data2Map5.put("unit", "件");
        data2Map5.put("total", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("jlgznum"));
        data2list.add(data2Map5);
        Map<String, Object> data2Map6 = new LinkedHashMap<>();
        data2Map6.put("name", "停电原因查询");
        data2Map6.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("tdyycxnum"));
        data2Map6.put("unit", "件");
        data2Map6.put("total", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("tdyycxnum"));
        data2list.add(data2Map6);
        Map<String, Object> data2Map7 = new LinkedHashMap<>();
        data2Map7.put("name", "未回复完成");
        data2Map7.put("value", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("whfwcnum"));
        data2Map7.put("unit", "件");
        data2Map7.put("total", isok || gzqxTjByZtVO == null ? "0" : gzqxTjByZtVO.get("whfwcnum"));
        data2list.add(data2Map7);
        data2.add(data2list);
        bottom.put("data2", data2);
        adcode.put("bottom", bottom);
        right.put(adcodeByDwbm,adcode);
    }

    /**
     * right--主动抢修管控
     *
     * @param right
     * @param dwbm
     */
    private void getRightZdqxgk(Map<String, Object> right, String dwbm) {
        //如果为辽宁省账号。需要查询辽宁省及下属地市所有数据
        if (CityCodeConstant.LIAONING.equals(dwbm)){
            //查询辽宁省数据
            String adcodeByDwbm = getAdcodeByDwbm(dwbm);
            getRightZdqxgkByDwbm(right, dwbm,adcodeByDwbm);
            //查询各地市数据
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.SHENYANG);
            getRightZdqxgkByDwbm(right, CityCodeConstant.SHENYANG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.DALIAN);
            getRightZdqxgkByDwbm(right, CityCodeConstant.DALIAN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.ANSHAN);
            getRightZdqxgkByDwbm(right, CityCodeConstant.ANSHAN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.FUSHUN);
            getRightZdqxgkByDwbm(right, CityCodeConstant.FUSHUN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.BENXI);
            getRightZdqxgkByDwbm(right, CityCodeConstant.BENXI,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.DANDONG);
            getRightZdqxgkByDwbm(right, CityCodeConstant.DANDONG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.JINZHOU);
            getRightZdqxgkByDwbm(right, CityCodeConstant.JINZHOU,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.YINGKOU);
            getRightZdqxgkByDwbm(right, CityCodeConstant.YINGKOU,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.FUXIN);
            getRightZdqxgkByDwbm(right, CityCodeConstant.FUXIN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.LIAOYANG);
            getRightZdqxgkByDwbm(right, CityCodeConstant.LIAOYANG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.PANJIN);
            getRightZdqxgkByDwbm(right, CityCodeConstant.PANJIN,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.TIELING);
            getRightZdqxgkByDwbm(right, CityCodeConstant.TIELING,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.CHAOYANG);
            getRightZdqxgkByDwbm(right, CityCodeConstant.CHAOYANG,adcodeByDwbm);
            adcodeByDwbm = getAdcodeByDwbm(CityCodeConstant.HULUDAO);
            getRightZdqxgkByDwbm(right, CityCodeConstant.HULUDAO,adcodeByDwbm);
        } else {
            String adcodeByDwbm = getAdcodeByDwbm(dwbm);
            getRightZdqxgkByDwbm(right, dwbm,adcodeByDwbm);
        }

    }

    private void getRightZdqxgkByDwbm(Map<String, Object> right, String dwbm, String adcodeByDwbm) {
        String date = getcurrentdate();
        Boolean flag = true;
        Map<String, Object> adcode = new LinkedHashMap<>();
        List<Map<String, Object>> top1 = new ArrayList<>();
        List<Map<String, Object>> top2 = new ArrayList<>();
        List<Map<String, Object>> top3 = new ArrayList<>();

        JSONObject gdsl = null;
        JSONObject sjtj = null;
        try {
            System.out.println("==============开始调用:" + "主动抢修管控接口" + "=================dwbm:"+ dwbm);
            JSONObject gzqx = commonFeign.getGzqx(date, dwbm);
            JSONObject data = gzqx.getJSONObject("data");
            gdsl = data.getJSONObject("gdsl");
            sjtj = data.getJSONObject("sjtj");
            flag = false;
        } catch (Exception e) {
            flag = true;
        }

        Map<String, Object> top1map1 = new LinkedHashMap<>();
        top1map1.put("label", "今日故障停电事件");
        top1map1.put("value", flag || gdsl == null ? 0 : gdsl.get("GZZS"));
        top1map1.put("unit", "件");
        top1.add(top1map1);
        Map<String, Object> top1map2 = new LinkedHashMap<>();
        top1map2.put("label", "平均停电时长");
        top1map2.put("value", flag || gdsl == null ? 0 : sjtj.get("PJTDSJ"));
        top1map2.put("unit", "小时");
        top1.add(top1map2);
        adcode.put("top1", top1);

        Map<String, Object> top2map1 = new LinkedHashMap<>();
        top2map1.put("label", "市供指 【未派单】");
        top2map1.put("value", flag || gdsl == null ? 0 : gdsl.get("WPD"));
        top2map1.put("unit", "件");
        top2.add(top2map1);
        Map<String, Object> top2map2 = new LinkedHashMap<>();
        top2map2.put("label", "县区 【未到达现场】");
        top2map2.put("value", flag || gdsl == null ? 0 : gdsl.get("WDD"));
        top2map2.put("unit", "件");
        top2.add(top2map2);
        Map<String, Object> top2map3 = new LinkedHashMap<>();
        top2map3.put("label", "县区 【未找到故障点】");
        top2map3.put("value", flag || gdsl == null ? 0 : gdsl.get("WZD"));
        top2map3.put("unit", "件");
        top2.add(top2map3);
        Map<String, Object> top2map4 = new LinkedHashMap<>();
        top2map4.put("label", "县区 【未完成修复】");
        top2map4.put("value", flag || gdsl == null ? 0 : gdsl.get("WWC"));
        top2map4.put("unit", "件");
        top2.add(top2map4);
        Map<String, Object> top2map5 = new LinkedHashMap<>();
        top2map5.put("label", "县区 【未完成复电】");
        top2map5.put("value", flag || gdsl == null ? 0 : gdsl.get("WFD"));
        top2map5.put("unit", "件");
        top2.add(top2map5);
        Map<String, Object> top2map6 = new LinkedHashMap<>();
        top2map6.put("label", "市供指 【故障报告未审核】");
        top2map6.put("value", flag || gdsl == null ? 0 : gdsl.get("WSH"));
        top2map6.put("unit", "件");
        top2.add(top2map6);
        Map<String, Object> top2map7 = new LinkedHashMap<>();
        top2map7.put("label", "市供指 【故障报告已归档】");
        top2map7.put("value", flag || gdsl == null ? 0 : gdsl.get("WGD"));
        top2map7.put("unit", "件");
        top2.add(top2map7);
        adcode.put("top2", top2);


        Map<String, Object> top3map1 = new LinkedHashMap<>();
        top3map1.put("label", "接单派发时长");
        top3map1.put("value", flag || sjtj == null ? 0 : sjtj.get("PJDJSJ"));
        top3map1.put("unit", "小时");
        top3.add(top3map1);
        Map<String, Object> top3map2 = new LinkedHashMap<>();
        top3map2.put("label", "到达现场时长");
        top3map2.put("value", flag || sjtj == null ? 0 : sjtj.get("PJDDXCSJ"));
        top3map2.put("unit", "小时");
        top3.add(top3map2);
        Map<String, Object> top3map3 = new LinkedHashMap<>();
        top3map3.put("label", "故障查找时长");
        top3map3.put("value", flag || sjtj == null ? 0 : sjtj.get("PJFXGZDSJ"));
        top3map3.put("unit", "小时");
        top3.add(top3map3);
        Map<String, Object> top3map4 = new LinkedHashMap<>();
        top3map4.put("label", "故障修复时长");
        top3map4.put("value", flag || sjtj == null ? 0 : sjtj.get("PJGZDXFSJ"));
        top3map4.put("unit", "小时");
        top3.add(top3map4);
        Map<String, Object> top3map5 = new LinkedHashMap<>();
        top3map5.put("label", "现场送电时长");
        top3map5.put("value", flag || sjtj == null ? 0 : sjtj.get("PJSDSJ"));
        top3map5.put("unit", "小时");
        top3.add(top3map5);
        adcode.put("top3", top3);
        right.put(adcodeByDwbm,adcode);
    }

    private String getcurrentdate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateFormat.format(new Date());
        System.out.println(date);
        return date;
    }

    private void getLeftChart(Map<String, Object> left, List<SstdjcVo> leftList) {
        int zhuxian = 0;
        int fenzhi = 0;
        int dangong = 0;
        if (leftList != null) {
            Map<String, List<SstdjcVo>> collect = leftList.stream().collect(Collectors.groupingBy(SstdjcVo::getKind));
            zhuxian = collect.get("主线") == null ? 0 : collect.get("主线").size();
            fenzhi = collect.get("分支") == null ? 0 : collect.get("分支").size();
            dangong = collect.get("单公") == null ? 0 : collect.get("单公").size();
        }
        //处理leftList 筛选各类别数量

        //Todo 主线  分支  单公  （未确定  专线）
        List<List<Map<String, Object>>> chart = new ArrayList<>();
        List<Map<String, Object>> chartdata = new ArrayList<>();
        Map<String, Object> chartMap = new LinkedHashMap<>();
        chartMap.put("name", "主线停电");
        chartMap.put("value", zhuxian);
        chartMap.put("unit", "件");
        chartMap.put("total", zhuxian);
        chartdata.add(chartMap);
        Map<String, Object> chartMap1 = new LinkedHashMap<>();
        chartMap1.put("name", "支线停电");
        chartMap1.put("value", fenzhi);
        chartMap1.put("unit", "件");
        chartMap1.put("total", fenzhi);
        chartdata.add(chartMap1);
        Map<String, Object> chartMap2 = new LinkedHashMap<>();
        chartMap2.put("name", "单台公变停电");
        chartMap2.put("value", dangong);
        chartMap2.put("unit", "件");
        chartMap2.put("total", dangong);
        chartdata.add(chartMap2);
//        Map<String, Object> chartMap3 = new LinkedHashMap<>();
//        chartMap3.put("name","专变停电");
//        chartMap3.put("value",collect.get("专变") == null?0:collect.get("专变").size());
//        chartMap3.put("unit","件");
//        chartMap3.put("total",collect.get("专变") == null?0:collect.get("专变").size());
//        chartdata.add(chartMap3);
        chart.add(chartdata);
        left.put("chart", chart);
    }

    private List<SstdjcVo> getLeftList(Map<String, Object> left) {
        //Todo 日期改成前端选择框获取
        String date = getcurrentdate();
        List<Map<String, Object>> list = new ArrayList<>();
        List<SstdjcVo> sstdjcVos = new ArrayList<>();
        //调用低电压文档接口
        try {
            System.out.println("==============开始调用:" + "实时停电监测接口" + "=================");
            JSONObject sstdlb = commonFeign.getSstdlb("012021", "", date, "0", date, "3");
            JSONArray array = sstdlb.getJSONObject("data").getJSONArray("tdxxlb");
            for (int i = 0; i < array.size(); i++) {
                SstdjcVo sstdjcVo = JSON.toJavaObject(array.getJSONObject(i), SstdjcVo.class);
                Map<String, Object> listmap = new LinkedHashMap<>();
                listmap.put("adcode", getAdcode(sstdjcVo.getDs()));
                listmap.put("type", sstdjcVo.getTdlx() + "/" + sstdjcVo.getKind());
                listmap.put("name", StringUtil.split(sstdjcVo.getXlmc(), '/')[0]);  //针对kind为单公的做特殊处理
                listmap.put("area", sstdjcVo.getDs() + " " + sstdjcVo.getQx());
                listmap.put("blackoutDate", sstdjcVo.getTdsj());
                listmap.put("recoverDate", sstdjcVo.getFdsj());
                listmap.put("blackoutTime", sstdjcVo.getCxsj() + "h");
                //与复电状态有关
                listmap.put("state", "未复电".equals(sstdjcVo.getFdzt()) ? "3" : "1"); // 0黄色填报 1绿色对号 2绿色叉号 3 红色闪电
                listmap.put("video", StringUtils.isBlank(sstdjcVo.getBkqbm()) ? "" : sstdjcVo.getBkqbm());// 为空则不显示
                //---------以下为前端筛选状态字段
                /*
                  objId: string,
                  // 全部 主线 分支 单台配变
                  kind: I_Second_Left_ListData_Kind,
                  // 未复电 全部
                  fdzt: I_Second_Left_ListData_fdzt,
                  // 已 未 报
                  whzt: I_Second_Left_ListData_whzt,
                  // √ ❌
                  zt: I_Second_Left_ListData_zt,
                  // 序号状态颜色
                  color: string,
                 */
                //obj_id 事件ID 用作穿透用
                listmap.put("objId", sstdjcVo.getObjid());
                //线路类型 全部：空 主线1 分支2 单公3
                listmap.put("kind", getChooseKind(sstdjcVo.getKind()));
                //复电状态 未复电0
                listmap.put("fdzt", "未复电".equals(sstdjcVo.getFdzt()) ? "0" : "");
                //维护状态 1维护完成 2维护未完成
                listmap.put("whzt", getChooseWhzt(sstdjcVo.getZtmc(), sstdjcVo.getTdlx()));
                //停电类型：1：故障 2：非故障
                listmap.put("tdlx", "故障".equals(sstdjcVo.getTdlx()) ? "1" : "2");
                //状态： 2 不属实 1非不属实
                listmap.put("zt", "3".equals(sstdjcVo.getZt()) ? "2" : "1");
                // 1红 未确认 2黄 待校核 3蓝 待维护 4绿 维护完成
                listmap.put("color", getChooseColor(sstdjcVo.getZtmc()));
                list.add(listmap);
            }
            sstdjcVos = JSONArray.parseArray(array.toJSONString(), SstdjcVo.class);
        } catch (Exception e) {
            Map<String, Object> listmap = new LinkedHashMap<>();
            listmap.put("type", StateConstant.BLANK_STRING);
            listmap.put("name", StateConstant.BLANK_STRING);  //针对kind为单公的做特殊处理
            listmap.put("area", StateConstant.BLANK_STRING);
            listmap.put("blackoutDate", StateConstant.BLANK_STRING);
            listmap.put("recoverDate", StateConstant.BLANK_STRING);
            listmap.put("blackoutTime", StateConstant.ZERO_STRING);
            listmap.put("state", 3); // 0黄色填报 1绿色对号 2绿色叉号 3 红色闪电
            listmap.put("video", "");// 为空则不显示
            //清空clear 只留一条数据
            list.clear();
            list.add(listmap);
            sstdjcVos = null;
        }
        left.put("list", list);
        return sstdjcVos;

    }

    private String getAdcodeByDwbm(String dwbm) {
        String adCode = null;
        switch (dwbm) {
            case "01202121501":
                adCode = "210100";
                break;
            case "01202121508":
                adCode = "210200";
                break;
            case "01202121502":
                adCode = "210300";
                break;
            case "01202121504":
                adCode = "210400";
                break;
            case "01202121505":
                adCode = "210500";
                break;
            case "01202121510":
                adCode = "210600";
                break;
            case "01202121503":
                adCode = "210700";
                break;
            case "01202121506":
                adCode = "210800";
                break;
            case "01202121507":
                adCode = "210900";
                break;
            case "01202121512":
                adCode = "211000";
                break;
            case "01202121513":
                adCode = "211100";
                break;
            case "01202121511":
                adCode = "211200";
                break;
            case "01202121509":
                adCode = "211300";
                break;
            case "01202121514":
                adCode = "211400";
                break;
            case "012021":
                adCode = "210000";
                break;
        }
        return adCode;
    }

    private String getAdcode(String ds) {
        String adCode = null;
        switch (ds) {
            case "沈阳":
                adCode = "210100";
                break;
            case "大连":
                adCode = "210200";
                break;
            case "鞍山":
                adCode = "210300";
                break;
            case "抚顺":
                adCode = "210400";
                break;
            case "本溪":
                adCode = "210500";
                break;
            case "丹东":
                adCode = "210600";
                break;
            case "锦州":
                adCode = "210700";
                break;
            case "营口":
                adCode = "210800";
                break;
            case "阜新":
                adCode = "210900";
                break;
            case "辽阳":
                adCode = "211000";
                break;
            case "盘锦":
                adCode = "211100";
                break;
            case "铁岭":
                adCode = "211200";
                break;
            case "朝阳":
                adCode = "211300";
                break;
            case "葫芦岛":
                adCode = "211400";
                break;
        }
        return adCode;
    }

    private String getChooseColor(String ztmc) {
        String colorState = null;
        switch (ztmc) {
            case "未确认":
                colorState = "#ff0000";
                break;
            case "待校核":
                colorState = "#fdb90c";
                break;
            case "待维护":
                colorState = "#0000FF";
                break;
            case "维护完成":
                colorState = "#40fd0c";
                break;
        }
        return colorState;
    }

    private String getChooseWhzt(String ztmc, String tdlx) {
        String whzt = null;
        if ("维护完成".equals(ztmc)) {
            whzt = "1";
        } else {
            whzt = "2";
        }
        return whzt;
    }

    private String getChooseKind(String kind) {
        String kindState = null;
        switch (kind) {
            case "主线":
                kindState = "1";
                break;
            case "分支":
                kindState = "2";
                break;
            case "单公":
                kindState = "3";
                break;
        }
        return kindState;
    }

}
